package com.cloud.basic.webservice.soapClient.Client;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.cloud.basic.webservice.soapClient.SoapAcess.CommonAccess;
import com.cloud.basic.webservice.soapClient.SoapAcess.SoapAccess;
import com.cloud.basic.webservice.soapClient.resolver.SoapResolver;


/**
 * soap1.1接口数据采集
 * @author eguid
 *
 */
public class SoapClient implements Client {
	private String xmlns=null;//命名空间
	private String header=null;//header参数
	private String body=null;//body参数
	private byte[] xml=null;//完整xml报文
	private String url=null;//请求路径
	private String soapAction=null;//调用方法
	//soap接口调用器
	private CommonAccess soapAaccess=new SoapAccess();//暂时写死

	public CommonAccess getSoapAaccess() {
		return soapAaccess;
	}
	public void setSoapAaccess(CommonAccess soapAaccess) {
		this.soapAaccess = soapAaccess;
	}

	public SoapClient() {
	}

	public SoapClient(String xmlns, String header, String body) {
		init(null, null, null, xmlns, header, body);
	}
	public SoapClient(String url,String soapAction,String xmlns,String header, String body) {
		init(null, url, soapAction, xmlns, header, body);
	}
	public SoapClient(byte[] xml,String url,String soapAction,String xmlns,String header, String body) {
		init(xml, url, soapAction, xmlns, header, body);
	}
	//初始化参数
	private void init(byte[] xml,String url,String soapAction,String xmlns, String header, String body){
		this.xml=xml;
		this.url=url;
		this.soapAction=soapAction;
		this.xmlns = xmlns;
		this.header = header;
		this.body = body;
	}

	@Override
	public String access(String xml,String urlString,String soapAction) throws IOException{
		return soapAaccess.commonAccess(xml,urlString,soapAction,CommonAccess.SOAP_CONTEXT_TYPE);
	}

	@Override
	public String access(String url,String soapAction) throws IOException{
		return access(splice(soapAction), url, soapAction);
	}

	@Override
	public String access(byte[] xml,String urlString,String soapAction) throws IOException{
		return soapAaccess.commonAccess(xml,urlString,soapAction,CommonAccess.SOAP_CONTEXT_TYPE);
	}

	@Override
	public String access(String soapAction) throws IOException {
		return access(this.url,soapAction);
	}

	@Override
	public String access() throws IOException{
		return access(this.url,this.soapAction);
	}

	@Override
	public String splice(String soapAction){
		String xml=CommonAccess.XML_TOP+CommonAccess.SOAP_TOP+CommonAccess.SOAP_HEADER1+CommonAccess.STAG+CommonAccess.CREDENTIAL;
		if(header==null||header.isEmpty()){
			xml+=CommonAccess.XMLNS+CommonAccess.QUOTE+xmlns+CommonAccess.QUOTE+CommonAccess.ETAG+CommonAccess.CRED_END+CommonAccess.SOAP_HEADER2;
		}else{
			xml+=CommonAccess.XMLNS+CommonAccess.QUOTE+xmlns+CommonAccess.QUOTE+CommonAccess.ETAG+header+CommonAccess.CRED_END+CommonAccess.SOAP_HEADER2;
		}

		if(body==null||body.isEmpty()){
			xml+=CommonAccess.SOAP_BODY1+CommonAccess.STAG+soapAction+CommonAccess.XMLNS+CommonAccess.QUOTE+xmlns+CommonAccess.QUOTE+CommonAccess.ETAG+CommonAccess.ENDTAG+soapAction+CommonAccess.ETAG+CommonAccess.SOAP_BODY2+CommonAccess.SOAP_END;
		}else{
			xml+=CommonAccess.SOAP_BODY1+CommonAccess.STAG+soapAction+CommonAccess.XMLNS+CommonAccess.QUOTE+xmlns+CommonAccess.QUOTE+CommonAccess.ETAG+body+CommonAccess.ENDTAG+soapAction+CommonAccess.ETAG+CommonAccess.ENDTAG+soapAction+CommonAccess.ETAG+CommonAccess.SOAP_BODY2+CommonAccess.SOAP_END;
		}
		System.err.println("soap1.1报文："+xml);
		return xml;
	}
	@Override
	public void setHeader(String header){
		this.header=header;
	}
	@Override
	public void setHeader(Map<String,String> map) {
		this.header=SoapResolver.mapToXml(map);
	}
	@Override
	public void setBody(Map<String, String> map) {
		this.body= SoapResolver.mapToXml(map);
	}
	@Override
	public void setBody(String body){
		this.body=body;
	}
	@Override
	public void setBody(List<String> list) {
		this.body=SoapResolver.listToXml(list);
	}
	@Override
	public void setBody(String... arr) {
		this.body=SoapResolver.listToXml(arr);
	}
	@Override
	public void setXmlns(String xmlns){
		this.xmlns=xmlns;
	}
	public String getSoapAction() {
		return soapAction;
	}
	@Override
	public void setSoapAction(String soapAction) {
		this.soapAction = soapAction;
	}
	public String getUrl() {
		return url;
	}
	@Override
	public void setUrl(String url) {
		this.url = url;
	}


}
